package com.rownext.kernel.logging.impl 
{
	import com.rownext.kernel.logging.ILogger;
	import com.rownext.kernel.logging.LogLevel;
	
	import flash.events.EventDispatcher;

	/**
	 * 当<code>ILogger</code>的fatal等log方法调用时调度此事件
	 * @eventType com.rownext.kernel.logging.impl.LogEvent.LOG
	 */
	[Event(name="log", type="com.rownext.kernel.logging.impl.LogEvent")]

	/**
	 * <code>ILogger</code>接口的默认实现类，负责调度<code>LogEvents</code>事件
	 */
	internal class Logger extends EventDispatcher implements ILogger
	{
		/**
		 * 构造函数
		 * @param name	日志记录器的名字
		 * @param level 日志记录器的级别
		 */
		public function Logger( name:String, level:LogLevel = null )
		{
			if( name == null || name == "" ) throw new ArgumentError( "Invalid argument." );
			if( level != null ) _level = level;
			_name = name;
		}

		/**
		 * @inheritDoc
		 */
		public function isTraceEnabled():Boolean 
		{
			return ( level.value <= LogLevel.TRACE.value );
		}

		/**
		 * @inheritDoc
		 */
		public function isDebugEnabled():Boolean 
		{
			return ( level.value <= LogLevel.DEBUG.value );
		}

		/**
		 * @inheritDoc
		 */
		public function isInfoEnabled():Boolean 
		{
			return ( level.value <= LogLevel.INFO.value );
		}

		/**
		 * @inheritDoc
		 */
		public function isWarnEnabled():Boolean 
		{
			return ( level.value <= LogLevel.WARN.value );
		}

		/**
		 * @inheritDoc
		 */
		public function isErrorEnabled():Boolean 
		{
			return ( level.value <= LogLevel.ERROR.value );
		}

		/**
		 * @inheritDoc
		 */
		public function isFatalEnabled():Boolean 
		{
			return ( level.value <= LogLevel.FATAL.value );
		}

		/**
		 * @inheritDoc
		 */
		public function trace( message:String ):void
		{
			if ( !isTraceEnabled() ) return;
			dispatch(LogLevel.TRACE, message);
		}

		/**
		 * @inheritDoc
		 */
		public function debug( message:String ):void 
		{
			if (!isDebugEnabled()) return;
			dispatch(LogLevel.DEBUG, message);
		}

		/**
		 * @inheritDoc
		 */
		public function info( message:String ):void 
		{
			if( !isInfoEnabled() ) return;
			dispatch( LogLevel.INFO, message );
		}

		/**
		 * @inheritDoc
		 */
		public function warn(message:String, error:Error = null):void 
		{
			if (!isWarnEnabled()) return;
			dispatch(LogLevel.WARN, message, error);
		}

		/**
		 * @inheritDoc
		 */
		public function error(message:String, error:Error = null):void 
		{
			if (!isErrorEnabled()) return;
			dispatch(LogLevel.ERROR, message, error);
		}

		/**
		 * @inheritDoc
		 */
		public function fatal(message:String, error:Error = null):void 
		{
			if (!isFatalEnabled()) return;
			dispatch(LogLevel.FATAL, message, error);
		}

		/**
		 * @inheritDoc
		 */
		public function get name():String 
		{
			return _name;
		}

		/**
		 * @inheritDoc
		 */	
		public function get level():LogLevel
		{
			return _level;
		}

		/**
		 * @inheritDoc
		 */
		public function set level(level:LogLevel):void
		{
			_level = level;
		}

		private function dispatch(level:LogLevel, message:String, error:Error = null):void 
		{
			super.dispatchEvent( new LogEvent( level, message, error ) );
		} 

		private var _name:String;
		private var _level:LogLevel = LogLevel.TRACE;

	}
}
